﻿var villagePhoto = {};

$(function() {
    //城区数据
    multipleRegionsCheckBox('#txtDistrict', 'hidDistrict', 1, false, ['hidArea'], '城区', '', '');//城区数据加载
    multipleRegionsCheckBox('#txtArea', 'hidArea', 2, '-1', ['hidDistrict'], '片区');
    $("#txtArea").focus(function () {
        if ($("#hidArea ul").find("li").length <= 0) {
            $("#hidArea").css("display", "none");
        }
    });
    //片区数据Start
    $('#hidDistrict :checkbox[name=chbhidDistrict]').click(function() {
        var districtKeyIds = [];
        $('#hidDistrict :checkbox[name=chbhidDistrict]:checked').each(function() {
            districtKeyIds.push($(this).val());
        });
        if (districtKeyIds.length <= 0) {
            $('#txtArea').val('片区');
        }

        multipleRegionsCheckBox('#txtArea', 'hidArea', 2, districtKeyIds, ['hidDistrict'], '片区');
    });
    //片区数据End

    //初始化EstateList的属性或方法Start  
    villagePhoto.getEstateUrl = function () {
        var districtKeyIds = '', areaKeyIds = '', estateName = [];
        $('#hidDistrict input[type=checkbox]:checked').each(function () {
            districtKeyIds += $(this).val() + ',';
        });
        $('#hidArea input[type=checkbox]:checked').each(function () {
            areaKeyIds += $(this).val() + ',';
        });
        estateName = $('#txtEstateNames').val();
        return '/property/estates-for-region-keyids-obtain-ajax?DistrictKeyIds=' + districtKeyIds.substr(0, districtKeyIds.length - 1) + '&AreaKeyIds=' + areaKeyIds.substr(0, areaKeyIds.length - 1) + '&EstateName=' + escape(estateName);
    };

    //楼盘自动完成及赋值隐藏域
    $("#txtEstateNames").autocomplete(villagePhoto.getEstateUrl, {
        width: 260,
        max: 1000,
        Default: 1,
        selectFirst: false,
        cacheLength: 0,
        multipleSeparator:'+',
        formatItem: function (row) {
            if (row.EstateFullName != null && row.EstateFullName != "") {
                return row.EstateFullName;
            } else {
                return row.EstateName;
            }
        },
        formatResult: function (row) {
            return row.EstateName;
        }
    }).result(function (a, b) {
        $("#hidEstateKeyId").val(b.KeyId);
        $("#txtEstateNames").attr('isexistbuild', 'true');
    });
    
    $('#txtEstateNames').bind('change', function () {
        $(this).attr('isexistbuild', 'true');
        $("#hidEstateKeyId").val('');
    });
    
});

function ValidateImage() {
    if ($("#txtEstateNames").val() == "") {
        $.OKDialogAgency('<div class="tc">请选择一个楼盘上传！</div>');
        return false;
    } else {
        if ($("#txtEstateNames").val().split("+").length > 1) {
            $.OKDialogAgency('<div class="tc">请选择一个楼盘上传！</div>');
            return false;
        }
    }
    if ($("#hidEstateKeyId").val() == "") {
        $.OKDialogAgency('<div class="tc">请从下拉框选择一个楼盘！</div>');
        return false;
    }
    if ($("#xqyjimgdiv").children().length > 1) {
        $.OKDialogAgency('<div class="tc">远景图只允许上传1张！</div>');
        return false;
    }
    if ($("#xqyjimgdiv").children().length == 0) {
        $.OKDialogAgency('<div class="tc">必须上传一张远景图！</div>');
        return false;
    }
    
    if ($("#xqdmimgdiv").children().length > 1) {
        $.OKDialogAgency('<div class="tc">大门只允许上传1张！</div>');
        return false;
    }
    if ($("#xqdmimgdiv").children().length == 0) {
        $.OKDialogAgency('<div class="tc">必须上传一张大门！</div>');
        return false;
    }
    
    if ($("#hjlhimgdiv").children().length > 5) {
        $.OKDialogAgency('<div class="tc">环境绿化最多上传5张！</div>');
        return false;
    }
    if ($("#hjlhimgdiv").children().length < 3 ) {
        $.OKDialogAgency('<div class="tc">环境绿化最少上传3张！</div>');
        return false;
    }

    if ($("#jcimgdiv").children().length > 5) {
        $.OKDialogAgency('<div class="tc">基础设施最多上传5张！</div>');
        return false;
    }
    if ($("#jcimgdiv").children().length < 3 ) {
        $.OKDialogAgency('<div class="tc">基础设施最少上传3张！</div>');
        return false;
    }

    if ($("#xqfkimgdiv").children().length > 2) {
        $.OKDialogAgency('<div class="tc">俯瞰图最多上传2张！</div>');
        return false;
    }
    if ($("#xqfkimgdiv").children().length == 0) {
        $.OKDialogAgency('<div class="tc">俯瞰图最少上传1张！</div>');
        return false;
    }
    
}
//点击保存按纽时,按纽隐藏,这个方法在表单提交时ajax.beginform里调用
villagePhoto.saveSuceeBegin = function () {
    $('#btn-village-photo').css('display', 'none');
};
//保存成功调用的方法
villagePhoto.saveSucee = function (data) {
    var saveResult = '';
    if (data == "no" || data.indexOf("[提示]") > -1) {
        saveResult = '<div id="realsafe" class="pop-box">' + data + '！<div class="tc"><a id="btnGoon" class="btn-big-orange mt20">确定</a></div></div>';
        $('#btn-village-photo').css('display', '');
    } else {
        saveResult = '<div id="realsafe" class="pop-box">成功上传小区图片！<div class="tc"><a id="btnGoon" class="btn-big-orange mt20" keyId="' + data + '">确定</a></div></div>';
    }

    $.dialog({
        id: "villagePhoto",
        lock: true,
        close: function () {
            if ($('#btnGoon').attr('keyId')) {
                window.location.href = '/estate/village-photo-audit-for-keyid?keyId=' + $('#btnGoon').attr('keyId');
            }
        },
        content: saveResult
    });

    $('#btnGoon').unbind('click');
    $('#btnGoon').click(function () {
        art.dialog.get('villagePhoto').close();
        if ($(this).attr('keyId')) {
            window.location.href = '/estate/village-photo-audit-for-keyid?keyId=' + $(this).attr('keyId');
        }

    });
};
